package com.gk.service.impl;

import com.alibaba.fastjson.JSON;
import com.gk.bean.*;
import com.gk.bean.qfdto.InStockBillDto;
import com.gk.bean.qfdto.OutStockBillDto;
import com.gk.bean.qfdto.OutStockBillIndexDto;
import com.gk.dao.OutStcokBillDao;
import com.gk.dao.ZxyUploadbillDao;
import com.gk.dao.ZxyUploadbillGuoyaoDao;
import com.gk.http.HttpApi;
import com.gk.http.entity.HttpCommon;
import com.gk.http.entity.HttpRequestParams;
import com.gk.http.entity.HttpResult;
import com.gk.service.OutBillService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OutBillServiceImpl implements OutBillService {

    @Autowired
    private OutStcokBillDao outStcokBillDao;

    @Autowired
    private ZxyUploadbillGuoyaoDao zxyUploadbillGuoyaoDao;

    @Override
    public HttpResult XsBillUpload(String billCode) {
        OutStockBillIndexDto indexDto = outStcokBillDao.getXsBillIndexInfo(billCode);
        List<OutStockBillDto> billDtos = outStcokBillDao.getXsBillInfo(billCode);
        HttpRequestParams httpRequestParams = new HttpRequestParams();
        httpRequestParams.setTrackNo(billCode);
        CommonDto commonDto = new CommonDto();
        T302HeaderBean t302HeaderBean = new T302HeaderBean();
        commonDto.getXml().setHeader(t302HeaderBean);
        commonDto.getXml().getContent().getDetail().setMain(indexDto);
        for (OutStockBillDto billDto : billDtos) {
            commonDto.getXml().getContent().getDetail().getSub().getSubline().add(billDto);
        }
        httpRequestParams.getData().add(commonDto);
        httpRequestParams.setTrackNo(indexDto.getExporderid());
        log.info("上传国控销售单信息json:" + JSON.toJSONString(httpRequestParams));
        HttpResult httpResult = HttpApi.HttpRequest2(httpRequestParams, HttpCommon.OUTSTOCKBILL_URL);
        log.info("上传国控销售单信息返回json:" + JSON.toJSONString(httpResult));
        //TODO 修改中间库数据
        if (httpResult.getCode().equals("0")) {
            int i = outStcokBillDao.updateOutQty0(billCode);
            log.info("销售单复核记录已经将OutQty置为0" + i);
            for (OutStockBillDto billDto : billDtos) {
                ZxyUploadbillGuoyao zxyUploadbillJzt = new ZxyUploadbillGuoyao();
                zxyUploadbillJzt.setBillcode(billCode);
                zxyUploadbillJzt.setOrd(Integer.parseInt(billDto.getExporderdtlid()));
                zxyUploadbillJzt.setBilltype(BillTypeCommon.saleBill);
                zxyUploadbillGuoyaoDao.updateFlags(zxyUploadbillJzt);
            }
        }
        return httpResult;
    }

    @Override
    public HttpResult CgBackBillUpload(String billCode) {
        OutStockBillIndexDto indexDto = outStcokBillDao.getCgBackBillIndexInfo(billCode);
        List<OutStockBillDto> billDtos = outStcokBillDao.getCgBackBillInfo(billCode);
        HttpRequestParams httpRequestParams = new HttpRequestParams();
        httpRequestParams.setTrackNo(billCode);
        CommonDto commonDto = new CommonDto();

        T302HeaderBean t302HeaderBean = new T302HeaderBean();

        commonDto.getXml().setHeader(t302HeaderBean);
        commonDto.getXml().getContent().getDetail().setMain(indexDto);
        for (OutStockBillDto billDto : billDtos) {
            commonDto.getXml().getContent().getDetail().getSub().getSubline().add(billDto);
        }
        httpRequestParams.getData().add(commonDto);
        httpRequestParams.setTrackNo(indexDto.getExporderid());
        log.info("上传国控采购退货信息json:" + JSON.toJSONString(httpRequestParams));
        HttpResult httpResult = HttpApi.HttpRequest2(httpRequestParams, HttpCommon.OUTSTOCKBILL_URL);
        log.info("上传国控采购退货信息返回json:" + JSON.toJSONString(httpResult));
        //TODO 修改中间库数据
        if (httpResult.getCode().equals("0")) {
            int i = outStcokBillDao.updateOutQty0(billCode);
            log.info("采购退货单复核记录已经将OutQty置为0" + i);
            for (OutStockBillDto billDto : billDtos) {
                ZxyUploadbillGuoyao zxyUploadbillJzt = new ZxyUploadbillGuoyao();
                zxyUploadbillJzt.setBillcode(billCode);
                zxyUploadbillJzt.setOrd(Integer.parseInt(billDto.getExporderdtlid()));
                zxyUploadbillJzt.setBilltype(BillTypeCommon.buyBackBill);
                zxyUploadbillGuoyaoDao.updateFlags(zxyUploadbillJzt);
            }
        }
        return httpResult;
    }

    @Override
    public HttpResult LoessBillUpload(String billCode) {
        OutStockBillIndexDto indexDto = outStcokBillDao.getLossBillIndex(billCode);
        List<OutStockBillDto> billDtos = outStcokBillDao.getLossBillDetails(billCode);
        HttpRequestParams httpRequestParams = new HttpRequestParams();
        httpRequestParams.setTrackNo(billCode);
        CommonDto commonDto = new CommonDto();
        T302HeaderBean t302HeaderBean = new T302HeaderBean();
        commonDto.getXml().setHeader(t302HeaderBean);
        commonDto.getXml().getContent().getDetail().setMain(indexDto);
        for (OutStockBillDto billDto : billDtos) {
            commonDto.getXml().getContent().getDetail().getSub().getSubline().add(billDto);
        }
        httpRequestParams.getData().add(commonDto);
        httpRequestParams.setTrackNo(indexDto.getExporderid());
        log.info("上传国控报损单json:" + JSON.toJSONString(httpRequestParams));
        HttpResult httpResult = HttpApi.HttpRequest2(httpRequestParams, HttpCommon.OUTSTOCKBILL_URL);
        log.info("上传国控报损单信息返回json:" + JSON.toJSONString(httpResult));
        //TODO 修改中间库数据
        return httpResult;
    }
}
